import allure
from selenium.webdriver.common.by import By
from pages.base_page import BasePage
from settings import ENV


class LoginPage(BasePage):
    #元素定位器
    #输入框——手机号
    input_phone = (By.XPATH, "//input[@id='phone_number']")
    #文本——手机号错误
    error_message_phone = (By.XPATH,"//span[@id='phone_number-errormessage']")
    #输入框——验证码
    input_vcode = (By.XPATH, "//input[@id='verification_code']")
    #文本——验证码错误
    text_vcod = (By.XPATH,"//span[@id='verification_code-errormessage']")
    #按钮——发送验证码
    bt_vcode = (By.XPATH, "//button[@type='button']")
    #超链接——注册
    a_signin = (By.XPATH, "//span[@class='_form-register-link-text_1svdg_24']")
    #按钮——登录
    bt_login = (By.XPATH, "//button[@type='submit']")
    #复选框——协议——外层用判断是否选中
    check_box_agreement1 = (By.XPATH, "//input[@type='checkbox']")
    # 复选框——协议——内层用于点击勾选
    check_box_agreement2 = (By.XPATH, "//span[@class='semi-checkbox-inner-display']")
    #超链接——用户协议
    a_agreement = (By.XPATH, "//a[contains(text(),'《用户协议》')]")
    #超链接——隐私政策
    a_privacy = (By.XPATH, "//a[contains(text(),'《隐私政策》')]")


    #用于判断复选框是否被选中的属性
    att_checkbox_selected = "aria-checked"


    def __init__(self,driver):
        super().__init__(driver)
        self.url = ENV.URL_ushannon_login_page_dev

    @allure.feature("打开网页")
    def open_login_page(self):
        #打开网页
        self.open_url(self.url)

    @allure.feature("填写手机号")
    def enter_phone(self, phone):
        #填写手机号
        self.send_keys(self.input_phone,phone)

    @allure.feature("填写验证码")
    def enter_vcode(self,vcode):
        #填写验证码
        self.send_keys(self.input_vcode,vcode)

    @allure.feature("点击注册")
    def click_signin(self):
        #点击注册
        self.l_click(self.a_signin)

    @allure.feature("点击登录")
    def click_login(self):
        #点击登录
        self.l_click(self.bt_login)

    @allure.feature("点击发送验证码")
    def click_send_vcode(self):
        #点击发送验证码
        self.l_click(self.bt_vcode)

    @allure.feature("点击勾选协议")
    def click_check_box_agreement(self):
        #点击勾选协议
        self.l_click(self.check_box_agreement2)

    @allure.feature("点击隐私协议")
    def click_a_privacy(self):
        #点击隐私协议
        self.l_click(self.a_privacy)

    @allure.feature("点击用户协议")
    def click_a_agreement(self):
        #点击用户协议
        self.l_click(self.bt_login)

    @allure.feature("判断复选框是否被选中")
    def is_check_box_agreement_selected(self) ->bool:
        #判断复选框是否被选中
        return self.get_att(locator=self.check_box_agreement1,attname=self.att_checkbox_selected) =="true"

    @allure.feature("返回手机号错误文本提示")
    def get_error_message_phone(self)->str:
        #返回手机号错误文本提示
        return self.get_text(locator=self.error_message_phone)

    @allure.feature("判断是否成功登录")
    def is_succeed_login(self):
        #判断是否成功登录
        return self.is_expected_url(ENV.URL_ushannon_home_page_dev)

    @allure.feature("获取手机号输入框内容")
    def get_phone_inputbox_content(self):
        #获取手机号输入框内容
        return self.get_text(locator=self.input_phone)

